package com.leihao.product.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leihao.common.core.exception.ServiceException;
import com.leihao.product.domian.MemberPrice;
import com.leihao.product.domian.ProductFullReduction;
import com.leihao.product.domian.ProductLadder;
import com.leihao.product.domian.Subject;
import com.leihao.product.domian.listRequest.ProductListRequest;
import com.leihao.product.domian.vo.ProductVo;
import com.leihao.product.mapper.ProductMapper;
import com.leihao.product.service.MemberPriceService;
import com.leihao.product.service.ProductFullReductionService;
import com.leihao.product.service.ProductLadderService;
import com.leihao.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private MemberPriceService memberPriceService;
    @Autowired
    private ProductLadderService productLadderService;
    @Autowired
    private ProductFullReductionService productFullReductionService;

    @Override
    public PageInfo<ProductVo> findList(ProductListRequest productListRequest) {
        if (productListRequest.getPageNum() == null || productListRequest.getPageSize() == null) {
            throw new ServiceException("参数不能为空!");
        }
        PageHelper.startPage(productListRequest.getPageNum(), productListRequest.getPageSize());
        List<ProductVo> list = productMapper.findList(productListRequest);
        PageInfo<ProductVo> productVoPageInfo = new PageInfo(list);
        return productVoPageInfo;
    }

    @Override
    @Transactional
    public void update(ProductVo productVo) {
        if (productVo.getProductId() == null || productVo.getProductId() <= 0) {
            throw new ServiceException("请正确输入商品编号!");
        }
        //关联专题  ->判空
        Integer[] subjectIds = productVo.getSubjectIds();
        if (subjectIds == null) {
            throw new ServiceException("至少选择一个关联专题!");
        }
        if (containsEmptyOrNULL(subjectIds)) {
            throw new ServiceException("关联专题中不能存在空值!");
        }
        //删除中间表
        productMapper.deleteSubjectmidden(productVo.getProductId());
        //TODO 添加商品专题中间表
        productMapper.addSubjectmidden(productVo.getProductId(), subjectIds);
        //关联优选  ->判空
        Integer[] preferenceIds = productVo.getPreferenceIds();
        if (preferenceIds == null) {
            throw new ServiceException("至少选择一个关联优选!");
        }
        if (containsEmptyOrNULL(preferenceIds)) {
            throw new ServiceException("关联优选中不能存在空值!");
        }
        //删除中间表
        productMapper.deletePreferencemidden(productVo.getProductId());
        //TODO 添加商品优选中间表
        productMapper.addPreferencemidden(productVo.getProductId(), preferenceIds);

        //服务保证多选框 ->判空
        Integer[] serviceGuaranteeIds = productVo.getServiceGuaranteeIds();
        if (serviceGuaranteeIds == null) {
            throw new ServiceException("至少选择一个服务保证!");
        }
        if (containsEmptyOrNULL(serviceGuaranteeIds)) {
            throw new ServiceException("服务保证中不能存在空值!");
        }
        //删除中间表
        productMapper.deleteServiceGuaranteemidden(productVo.getProductId());
        //TODO 添加商品服务保证中间表
        productMapper.addServiceGuaranteemidden(productVo.getProductId(), serviceGuaranteeIds);


        /**
         * 修改商品为无优惠方式
         */
        if (productVo.getPreferentialMode() == 0) {
            productVo.setStartTime(null);
            productVo.setEndTime(null);
            productVo.setProductPrice(null);
            //删除满减价格
            productFullReductionService.deleteFullReductionList(productVo.getProductId());
            //删除阶梯价格
            productLadderService.deleteLadderList(productVo.getProductId());
            //删除会员价格
            memberPriceService.deleteMemberPriceList(productVo.getProductId());
        }
        if (productVo.getPreferentialMode() == 1) {
            if (productVo.getStartTime() == null || productVo.getEndTime() == null) {
                throw new ServiceException("请选择优惠时间!");
            }
            if (productVo.getProductPrice() == null || productVo.getProductPrice().doubleValue() <= 0) {
                throw new ServiceException("请正确输入优惠价格!");
            }
            //删除满减价格
            productFullReductionService.deleteFullReductionList(productVo.getProductId());
            //删除阶梯价格
            productLadderService.deleteLadderList(productVo.getProductId());
            //删除会员价格
            memberPriceService.deleteMemberPriceList(productVo.getProductId());
            productMapper.update(productVo);
        }
        if (productVo.getPreferentialMode() == 3) {
            /**
             * 修改阶梯价格优惠方式
             */
            //1.删除之前的阶梯价格
            productLadderService.deleteLadderList(productVo.getProductId());
            //2.添加阶梯价格
            List<ProductLadder> ladderList = productVo.getLadderList();
            if (CollectionUtils.isEmpty(ladderList)) {
                throw new ServiceException("请正确输入阶梯价格!");
            }
            List<ProductLadder> collect = ladderList.stream().map(productLadder -> {
                productLadder.setProductId(productVo.getProductId());
                return productLadder;
            }).collect(Collectors.toList());
            //TODO 添加阶梯价格
            productLadderService.addBatch(collect);
        }

        if (productVo.getPreferentialMode() == 4) {
            /**
             * 修改满减价格优惠方式
             */
            //1.删除之前的满减价格
            productFullReductionService.deleteFullReductionList(productVo.getProductId());
            //2.添加满减价格
            List<ProductFullReduction> fullReductionList = productVo.getFullReductionList();
            if (CollectionUtils.isEmpty(fullReductionList)) {
                throw new ServiceException("请正确输入满减价格!");
            }
            List<ProductFullReduction> collect = fullReductionList.stream().map(productFullReduction -> {
                productFullReduction.setProductId(productVo.getProductId());
                return productFullReduction;
            }).collect(Collectors.toList());
            productFullReductionService.addBatch(collect);
        }
        if (productVo.getPreferentialMode() == 2){
            /**
             * 修改会员价格优惠方式
             */
            //1.删除之前的会员价格
            memberPriceService.deleteMemberPriceList(productVo.getProductId());
            //2.添加会员价格
            List<MemberPrice> memberPriceList = productVo.getMemberPriceList();
            if (memberPriceList != null && memberPriceList.size() > 0) {
                List<MemberPrice> collect = memberPriceList.stream().map(memberPrice -> {
                    memberPrice.setProductId(productVo.getProductId());
                    return memberPrice;
                }).collect(Collectors.toList());
                //TODO 添加会员价格
                memberPriceService.addBatch(collect);
            }
        }
        productMapper.update(productVo);
        //TODO 修改SKU
    }

    @Override
    @Transactional
    public void add(ProductVo productVo) {
        //先判断该商品选择的哪种优惠方式 ->判空
        if (productVo.getPreferentialMode() == null) {
            productVo.setPreferentialMode(0);
        }
        //商品分类级联  ->判空
        Integer[] productTypeIdList = productVo.getProductTypeIdList();
        if (productTypeIdList == null) {
            throw new ServiceException("至少选择一个商品分类!");
        }
        if (containsEmptyOrNULL(productTypeIdList)) {
            throw new ServiceException("商品分类中不能存在空值!");
        }
        //取到分类级联的最后一级编号,并添加至商品表
        Integer productTypeId = productTypeIdList[productTypeIdList.length - 1];
        productVo.setProductTypeId(productTypeId);
        Integer preferentialMode = productVo.getPreferentialMode();
        //满减价格列表
        List<ProductFullReduction> fullReductionList = productVo.getFullReductionList();
        //阶梯价格列表
        List<ProductLadder> ladderList = productVo.getLadderList();
        //会员价格列表
        List<MemberPrice> memberPriceList = productVo.getMemberPriceList();

        if (preferentialMode == 0){
            productVo.setStartTime(null);
            productVo.setEndTime(null);
            productVo.setProductPrice(null);
        }
        /**
         * 特惠促销
         */
        if (preferentialMode == 1) {//特惠促销
            if (productVo.getStartTime() == null) {
                throw new ServiceException("特惠促销开始时间不能为空!");
            }
            if (productVo.getEndTime() == null) {
                throw new ServiceException("特惠促销结束时间不能为空!");
            }
            if (productVo.getProductPrice() == null || productVo.getProductPrice().doubleValue() == 0) {
                throw new ServiceException("特惠促销价格不能为0!");
            }
        }
        productMapper.add(productVo);

        /**
         * 会员价格
         */
        //新增商品的主键ID

        Integer productId = productVo.getProductId();

        if (preferentialMode == 2){
            if (memberPriceList == null || memberPriceList.size() == 0) {
                throw new ServiceException("会员价格列表不能为空!");
            }
            //TODO 先拿到商品编号 添加会员价格
            List<MemberPrice> collect = memberPriceList.stream().map(memberPrice -> {
                memberPrice.setProductId(productId);
                return memberPrice;
            }).collect(Collectors.toList());
            memberPriceService.addBatch(collect);
        }



        /**
         * 阶梯价格
         */
        if (preferentialMode == 3){
            if (ladderList == null || ladderList.size() == 0) {
                throw new ServiceException("阶梯价格列表不能为空!");
            }
            //TODO 先拿到商品编号 添加阶梯价格
            List<ProductLadder> productLadderCollect = ladderList.stream().map(productLadder -> {
                productLadder.setProductId(productId);
                return productLadder;
            }).collect(Collectors.toList());
            productLadderService.addBatch(productLadderCollect);
        }


        /**
         * 满减促销
         */
        if (preferentialMode == 4){
            if (fullReductionList == null || fullReductionList.size() == 0) {
                throw new ServiceException("满减促销列表不能为空!");
            }
            //TODO 先拿到商品编号 添加满减促销
            List<ProductFullReduction> fullReductionCollect = fullReductionList.stream().map(productFullReduction -> {
                productFullReduction.setProductId(productId);
                return productFullReduction;
            }).collect(Collectors.toList());
            productFullReductionService.addBatch(fullReductionCollect);
        }

        //关联专题  ->判空
        Integer[] subjectIds = productVo.getSubjectIds();
        if (subjectIds == null) {
            throw new ServiceException("至少选择一个关联专题!");
        }
        if (containsEmptyOrNULL(subjectIds)) {
            throw new ServiceException("关联专题中不能存在空值!");
        }

        //TODO 添加商品专题中间表
        productMapper.addSubjectmidden(productVo.getProductId(), subjectIds);

        //服务保证多选框 ->判空
        Integer[] serviceGuaranteeIds = productVo.getServiceGuaranteeIds();
        if (serviceGuaranteeIds == null) {
            throw new ServiceException("至少选择一个服务保证!");
        }
        if (containsEmptyOrNULL(serviceGuaranteeIds)) {
            throw new ServiceException("服务保证中不能存在空值!");
        }
        //TODO 添加商品服务保证中间表
        productMapper.addServiceGuaranteemidden(productVo.getProductId(), serviceGuaranteeIds);

        Integer[] preferenceIds = productVo.getPreferenceIds();
        if (preferenceIds == null) {
            throw new ServiceException("至少选择一个商品优选!");
        }
        if (containsEmptyOrNULL(preferenceIds)) {
            throw new ServiceException("商品优选中不能存在空值!");
        }
        //TODO 添加商品优选主题中间表
        productMapper.addPreferencemidden(productVo.getProductId(), preferenceIds);

        //TODO 添加SKU

    }


    /**
     * 判断数组中是否存在空值
     *
     * @param array
     * @return
     */
    private boolean containsEmptyOrNULL(Integer[] array) {
        return Arrays.stream(array)
                .anyMatch(item -> item == null);
    }

    @Override
    public void delete(Integer productId) {
        if (productId == null || productId <= 0) {
            throw new ServiceException("请正确选择要删除的编号!");
        }
        productMapper.delete(productId);
    }

    /**
     * 查询服务保证中间表
     */
    @Override
    public Integer[] findByServiceGuaranteemidden(Integer productId) {
        return productMapper.findByServiceGuaranteemidden(productId);
    }

    @Override
    public Integer[] findBySubjectmidden(Integer productId) {
        return productMapper.findBySubjectmidden(productId);
    }

    @Override
    public void updateStatus(ProductVo productVo) {
        if (productVo == null) {
            throw new ServiceException("商品对象不能为空!");
        }
        if (productVo.getProductId() == null || productVo.getProductId() <= 0) {
            throw new ServiceException("请正确选择商品编号!");
        }
        productMapper.update(productVo);
    }

    @Override
    public List<ProductLadder> findByLadderList(Integer productId) {
        if (productId == null || productId <= 0) {
            throw new ServiceException("请正确选择商品编号!");
        }
        return productMapper.findByLadderList(productId);
    }

    @Override
    public List<MemberPrice> findByMemberPriceList(Integer productId) {
        if (productId == null || productId <= 0) {
            throw new ServiceException("请正确选择商品编号!");
        }
        return productMapper.findByMemberPriceList(productId);
    }

    @Override
    public List<ProductFullReduction> findByFullReductionList(Integer productId) {
        if (productId == null || productId <= 0) {
            throw new ServiceException("请正确选择商品编号!");
        }
        return productMapper.findByFullReductionList(productId);
    }

    @Override
    public Integer[] findByPreferenceMidden(Integer productId) {
        if (productId == null || productId <= 0) {
            throw new ServiceException("请正确选择商品编号!");
        }
        return productMapper.findByPreferenceMidden(productId);
    }
}
